//
//  WPTabBarViewController.m
//  01 微博
//
//  Created by zhoup on 16/5/9.
//  Copyright © 2016年 zhoup. All rights reserved.
//

#import "WPTabBarViewController.h"
#import "WPTabbar.h"
#import "DiscoverViewController.h"
#import "HomeViewController.h"
#import "ProfileViewController.h"
#import "MessageViewController.h"
#import "HomeViewController.h"

@interface WPTabBarViewController ()

@end

@implementation WPTabBarViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    
    WPTabbar *tabbar = [[WPTabbar alloc]init];
    
    [self setValue:tabbar forKey:@"tabBar"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        DDLogError(@"%s %d",__func__,__LINE__);
    }
    return self;
}

//控制器没有这个方法
//- (instancetype)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        DDLogError(@"%s %d",__func__,__LINE__);
//
//    }
//    return self;
//}
//控制器器的init方法默认会掉到initWithNibName，我们在initWithNibName
//方法中实现逻辑
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIViewController *home = [self addSubController:@"tabbar_home" selectedImageName:@"tabbar_home_selected" titleName:@"首页" withClass:[HomeViewController class]];

         UIViewController *message = [self addSubController:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected" titleName:@"消息" withClass:[MessageViewController class]];
        
        UIViewController *discover = [self addSubController:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected" titleName:@"发现" withClass:[DiscoverViewController class]];
        
        UIViewController *profile = [self addSubController:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected" titleName:@"我" withClass:[ProfileViewController class]];
        
        self.viewControllers = @[home, message, discover, profile];
        DDLogError(@"%s %d",__func__,__LINE__);
    }
    return self;
}

-(UIViewController *)addSubController:(NSString *)imageName selectedImageName:(NSString *)selectedImageName titleName:(NSString *)name withClass:(Class)className
{
    UIViewController *vc;
    if([NSStringFromClass(className) isEqualToString:@"HomeViewController"])
    {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"HomeViewController" bundle:nil];
        
        vc = [sb instantiateInitialViewController];
    }else
    {
        vc = [[className alloc]init];
        
    }
        
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    vc.tabBarItem.image=[UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage=[self renderImage:selectedImageName];
//    [vc.view setBackgroundColor:[UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]];
    
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
    vc.title=name;
    
    return nav;
   
}

-(UIImage *)renderImage:(NSString *)imageName
{
    UIImage *newImage =  [UIImage imageNamed:imageName];
    // 2.告诉系统原样显示
    newImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return newImage;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
